<template>
  <ul class="list">
    <li
      class="item"
      v-for="(item,index) in letters"
      :key="index"
      @click="handleLetterClick"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
      :ref="item"
    >{{ item }}</li>
  </ul>
</template>

<script>
export default {
  name: "Alphabet",
  props: {
    cities: Object
  },
  data() {
    return {
      //   abc: []
      touchStatus: false,
      startY: 0,
      timer: null
    };
  },
  updated() {
    //A元素对应顶部的高度
    this.startY = this.$refs["A"][0].offsetTop;
  },
  methods: {
    handleLetterClick(e) {
      let abc = [];
      for (let i in this.cities) {
        abc.push(i);
      }
      this.$emit("change", e.target.innerText); //发送
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      // 当前手指到最顶部的高度-头部高度
      let touchY = e.touches[0].clientY - 79;
      //（touchY - startY）手指到A顶部的高度/20(每个字母的高度)=当前第几个字母
      let index = Math.floor((touchY - this.startY) / 16.8);
      if (index >= 0 && index < this.letters.length) {
        this.$emit("change", this.letters[index]);
      }
    },
    handleTouchEnd() {
      this.touchStatus = false;
    }
  },
  computed: {
    letters() {
      let letters = [];
      for (let i in this.cities) {
        letters.push(i);
      }
      console.log(letters);
      return letters;
    }
  }
};
</script>

<style lang="stylus" scoped>
.list {
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  top: 1.58rem;
  right: 0;
  bottom: 0;
  width: 0.44rem;

  // background :red
  .item {
    line-height: 0.4rem;
    text-align: center;
    color: $bgColor;
  }
}
</style>